package com.lcxw.hxzbadmin.controller.unioncontroller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcxw.hxzbadmin.entity.requestbean.DealUnionIncomeReq;
import com.lcxw.hxzbadmin.entity.requestbean.GetUnionIncomeListReq;
import com.lcxw.hxzbadmin.entity.responseBean.ResponseData;
import com.lcxw.hxzbadmin.entity.responseBean.UnionIncomeInfo;
import com.lcxw.hxzbapi.entity.pojo.IncomeUnion;
import com.lcxw.hxzbapi.entity.pojo.Union;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.IncomeUnionService;
import com.lcxw.hxzbapi.service.UnionService;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class UnionIncomeRealization {

    @Resource
    IncomeUnionService incomeUnionService;

    @Resource
    UnionService unionService;


    //获取工会收入记录方法
    public Response getUnionIncomeList(GetUnionIncomeListReq getUnionIncomeListReq) {

        String term = getUnionIncomeListReq.getTerm();
        Integer unionId = getUnionIncomeListReq.getUnionId();
        String status = getUnionIncomeListReq.getStatus();
        int pageNum = getUnionIncomeListReq.getPageNum();
        int pageSize = getUnionIncomeListReq.getPageSize();
        IPage<IncomeUnion> incomeUnionIPage = incomeUnionService
            .selectList(unionId, term, status, pageNum, pageSize);
        int total = (int) incomeUnionIPage.getTotal();
        List<IncomeUnion> records = incomeUnionIPage.getRecords();
        List<UnionIncomeInfo> list = new ArrayList<>();

        for (IncomeUnion incomeUnion : records
        ) {
            if (incomeUnion.getIncome() > 0) {
                UnionIncomeInfo unionIncomeInfo = new UnionIncomeInfo();
                int _unionId = incomeUnion.getUnionId();
                Union union = unionService.selectById(_unionId);
                BeanUtils.copyProperties(incomeUnion, unionIncomeInfo);
                unionIncomeInfo.setBankName(union.getBankName());
                unionIncomeInfo.setCompany(union.getCompany());
                unionIncomeInfo.setPublicAccounts(union.getPublicAccounts());
                unionIncomeInfo.setCompanyLicense(union.getCompanyLicense());
                list.add(unionIncomeInfo);
            }

        }

        ResponseData responseData = new ResponseData();
        responseData.setTotal(list.size());
        responseData.setList(list);
        return Response.ok(responseData);

    }

    public Response dealUnionIncome(DealUnionIncomeReq dealUnionIncomeReq) {

        int id = dealUnionIncomeReq.getId();
        String decision = dealUnionIncomeReq.getDecision();
        String reason = dealUnionIncomeReq.getReason();
        String orderNum = dealUnionIncomeReq.getOrderNum();
        String transferType = dealUnionIncomeReq.getTransferType();
        if (decision.equals("refuse") && StringUtil.isEmpty(reason)) {
            return Response.error(ErrorCode.INVALID_PARAM, "拒绝打款必须携带原因。");
        }
        if (decision.equals("agree") && StringUtil.isEmpty(orderNum)) {
            return Response.error(ErrorCode.INVALID_PARAM, "打款成功必须携带转账单号。");
        }
        IncomeUnion incomeUnion = incomeUnionService.selectById(id);
        if (incomeUnion == null) {
            return Response.error(ErrorCode.DATA_NOT_EXIST, "处理的数据不存在");
        }
        if (decision.equals("agree")) {
            incomeUnion.setStatus("1");
            incomeUnion.setTransferType(transferType);
            incomeUnion.setTransferTime(StringUtil.getDateTime());
            incomeUnion.setOrderNum(orderNum);
            incomeUnionService.updateById(incomeUnion);
        }
        if (decision.equals("refuse")) {
            incomeUnion.setStatus("2");
            incomeUnion.setReason(reason);
            incomeUnionService.updateById(incomeUnion);
        }
        return Response.ok();

    }

    public List<String> getTerms() {
        return incomeUnionService.selectTerms();
    }
}
